package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
	"time"
)

// @Summary 添加案件日志
// @Tags 案件管理
// @Accept plain
// @Produce plain
// @Param CreateCaseLog body dto.CaseLogsListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/log/create [post]
func CreateCaseLog(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	caseLogsReq := dto.CaseLogsReq{}
	if err := c.Bind(&caseLogsReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseLogsReq.CaseId == 0 {
		resp.Code = 400
		resp.Message = "案件编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	wzjCaseLogs := models.WzjCaseLogs{}
	_ = utils.MapTo(caseLogsReq, &wzjCaseLogs)
	wzjCaseLogs.LogDate = time.Now().Unix()
	wzjCaseLogs.LogUserId = int64(userid)

	_case := services.CaseService{}
	if wzjCaseLogs.Id > 0 {
		boolRes, err := _case.UpdateCaseLog(wzjCaseLogs)
		if err == nil && boolRes {
			resp.Message = "更新案件日志成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("更新案件日志失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "更新案件日志失败"
		return c.JSON(http.StatusOK, resp)

	} else {
		boolRes, err := _case.CreateCaseLog(wzjCaseLogs)
		if err == nil && boolRes {
			resp.Message = "创建案件日志成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("创建案件日志失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "创建案件日志失败"
		return c.JSON(http.StatusOK, resp)
	}
}

// @Summary 获取当前案件条件的信息
// @Tags 案件管理
// @Accept plain
// @Produce plain
// @Param CaseLogList body dto.CaseLogsListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/log/list [get]
func CaseLogList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	caseLogsListReq := dto.CaseLogsListReq{}
	if err := c.Bind(&caseLogsListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	_case := services.CaseService{}
	respData, respPageCount, err := _case.GetCaseLogListByCondition(caseLogsListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.CaseLogsReq
	utils.MapTo(respData, &data)
	resp.Data = data

	return c.JSON(http.StatusOK, resp)
}

// @Summary 根据id获取合同详情
// @Tags 案件管理
// @Accept plain
// @Produce plain
// @Param logId query string true "案件日志id"
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/log/get [get]
func GetCaseLogInfo(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	logId, _ := strconv.Atoi(c.QueryParam("logId"))
	if logId == 0 {
		resp.Code = 400
		resp.Message = "案件日志编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	_case := services.CaseService{}

	wzjCaseLogs, err := _case.GetCaseLogById(logId)
	if err != nil {
		resp.Code = 400
		resp.Message = "根据案件日志编号查询日志信息失败"
		return c.JSON(http.StatusOK, resp)
	}
	//格式化
	caseLogsReq := dto.CaseLogsReq{}
	_ = utils.MapTo(wzjCaseLogs, &caseLogsReq)

	resp.Data = caseLogsReq
	return c.JSON(http.StatusOK, resp)
}
